import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
import { REDIRECT_URI, WEIXIN_APPID, WEIXIN_SECRET } from "./config";
import OAuth2Window, { loadJS, toQuery } from "./utils";
export var WChatAuthInner = /*#__PURE__*/ function _target() {
    "use strict";
    function WChatAuthInner(containerId) {
        _class_call_check(this, WChatAuthInner);
        _define_property(this, "load", window.WxLogin ? true : false);
        if ((_instanceof(this, WChatAuthInner) ? this.constructor : void 0) !== WChatAuthInner) {
            return;
        }
        if (!WChatAuthInner._instance) {
            WChatAuthInner._instance = this;
        }
        this.containerId = containerId;
        this.loadWXSdk();
        return WChatAuthInner._instance;
    }
    _create_class(WChatAuthInner, [
        {
            key: "loadWXSdk",
            value: function loadWXSdk(cb) {
                var _this = this;
                loadJS("http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js", function() {
                    var _this1, _this_onLoad;
                    _this.onLoaded();
                    _this.load = true;
                    (_this_onLoad = (_this1 = _this).onLoad) === null || _this_onLoad === void 0 ? void 0 : _this_onLoad.call(_this1);
                    cb();
                });
            }
        },
        {
            key: "getCode",
            value: function getCode() {
                if (!window.WxLogin) return;
                var WxLogin = window.WxLogin;
                var obj = new WxLogin({
                    appid: WEIXIN_APPID,
                    scope: "snsapi_login",
                    redirect_uri: REDIRECT_URI,
                    id: this.containerId || "wxLogin",
                    self_redirect: true,
                    state: {}
                });
                console.log(obj);
            }
        },
        {
            key: "getAccessToken",
            value: function getAccessToken() {}
        },
        {
            key: "getRefreshToken",
            value: function getRefreshToken() {}
        },
        {
            key: "getOnaAccessToken",
            value: function getOnaAccessToken() {}
        },
        {
            key: "onLoaded",
            value: function onLoaded(cb) {
                cb === null || cb === void 0 ? void 0 : cb();
            }
        }
    ], [
        {
            key: "getInstance",
            value: function getInstance(containerId) {
                if (!WChatAuthInner._instance) {
                    WChatAuthInner._instance = new WChatAuthInner(containerId);
                }
                return WChatAuthInner._instance;
            }
        },
        {
            key: "init",
            value: function init(cb) {
                var _this = this;
                return new Promise(function(resolve, reject) {
                    var wx = new _this("wxinxi");
                    if (!wx.load) {
                        wx.loadWXSdk(function() {
                            cb === null || cb === void 0 ? void 0 : cb(wx);
                            resolve(wx);
                        });
                    } else {
                        cb === null || cb === void 0 ? void 0 : cb(wx);
                        resolve(wx);
                    }
                }).catch(function(err) {
                    console.error("错误");
                });
            }
        }
    ]);
    return WChatAuthInner;
}();
_define_property(WChatAuthInner, "_instance", null);
var wChatAuthInner = WChatAuthInner.getInstance("wxinxi");
export { wChatAuthInner };
var WChatAuth = /*#__PURE__*/ function _target() {
    "use strict";
    function WChatAuth() {
        var _this = this;
        _class_call_check(this, WChatAuth);
        _define_property(this, "load", true);
        _define_property(this, "getCode", function() {
            return new Promise(function(resolve, reject) {
                /** 微信回调域名得一模一样 */ OAuth2Window.open("weixinlogin", "https://open.weixin.qq.com/connect/qrconnect?".concat(toQuery({
                    appid: WEIXIN_APPID,
                    redirect_uri: "http://www.qieryideas.com:3001/",
                    response_type: "code",
                    scope: "snsapi_login",
                    state: "{}"
                })), {
                    width: "600",
                    height: "600"
                }).then(function(res) {
                    resolve(res);
                }, reject);
            });
        });
        _define_property(this, "getAccessToken", function() {
            return new Promise(function(resolve, reject) {
                try {
                    _this.getCode().then(function(res) {
                        var code = res.code;
                        window.open("https://api.weixin.qq.com/sns/oauth2/access_token?".concat(toQuery({
                            appid: WEIXIN_APPID,
                            secret: WEIXIN_SECRET,
                            code: code,
                            grant_type: "authorization_code"
                        })));
                        return;
                        fetch("https://api.weixin.qq.com/sns/oauth2/access_token?".concat(toQuery({
                            appid: WEIXIN_APPID,
                            secret: WEIXIN_SECRET,
                            code: code,
                            grant_type: "authorization_code"
                        }))).then(function(wres) {
                            return wres.json();
                        }).then(function(data) {
                            console.log(data);
                        });
                    });
                } catch (err) {
                    reject(err);
                }
            });
        });
        if ((_instanceof(this, WChatAuth) ? this.constructor : void 0) !== WChatAuth) {
            return;
        }
        if (!WChatAuth._instance) {
            WChatAuth._instance = this;
        }
        /** 加载sdk */ return WChatAuth._instance;
    }
    _create_class(WChatAuth, [
        {
            key: "init",
            value: function init() {
                return new Promise(function(resolve) {
                    return resolve(WChatAuth._instance);
                });
            }
        }
    ], [
        {
            key: "getInstance",
            value: function getInstance() {
                if (!WChatAuth._instance) {
                    WChatAuth._instance = new WChatAuth();
                }
                return WChatAuth._instance;
            }
        }
    ]);
    return WChatAuth;
}();
_define_property(WChatAuth, "_instance", null);
export default WChatAuth.getInstance();

 //# sourceMappingURL=weixin.js.map